package cn.bankAutoOffice.controller;

import cn.bankAutoOffice.common.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 金健伟
 * @DATE 2023/4/9 16:11
 * 功能说明：
 */

@RestController
public class LoginAndLogoutController {

    @GetMapping("/index")
    public String index(){
        System.out.println("我进来了");
        return "index";
    }

    @PostMapping("/login")
    public Result login(@RequestParam("empCode") String empCode, @RequestParam("password") String password){
        if (!("".equals(empCode) && "".equals(password))){
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(empCode,password);
            try {
                subject.login(token);
            } catch (UnknownAccountException var6) {
                return Result.failure("账号不存在");
            } catch (LockedAccountException var7) {
                return Result.failure("账号被锁定");
            } catch (IncorrectCredentialsException var8) {
                return Result.failure("密码不匹配");
            }
            return Result.success(subject.getPrincipal());
        } else {
            return Result.failure("请输入账号或密码");
        }
    }

    @GetMapping("/logout")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.success();
    }

    @GetMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "403";
    }

}
